package aex.action;

import java.io.File;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import aex.domain.Article;
import aex.domain.ArticleCategory;
import aex.domain.Puser;
import aex.domain.ShowBox;
import aex.domain.ShowPoint;
import aex.service.ArticleCategoryService;
import aex.service.ArticleService;
import aex.service.CategoryEditerService;
import aex.service.CommentArticleService;
import aex.service.MedicinesService;
import aex.service.PuserService;
import aex.service.ShowBoxService;
import aex.service.ShowPointService;
import aex.util.CU;
import aex.util.PageBean;

import com.opensymphony.xwork2.ModelDriven;

@Controller("ArticleManagerAction")
@Scope("prototype")
public class ArticleManagerAction extends BaseActon  implements ModelDriven<Article>{

	private static final long serialVersionUID = 1L;


	@Resource(name = ArticleCategoryService.SERVICE_NAME)
	ArticleCategoryService acService;
	
	@Resource(name = CategoryEditerService.SERVICE_NAME)
	CategoryEditerService ceService;
	
	@Resource(name = MedicinesService.SERVICE_NAME)
	MedicinesService medicinesService;
	
	@Resource(name = PuserService.SERVICE_NAME)
	PuserService puserService;
	
	
	@Resource(name = ArticleService.SERVICE_NAME)
	ArticleService artService;

	@Resource(name = CommentArticleService.SERVICE_NAME)
	CommentArticleService comaService;

	@Resource(name = ShowBoxService.SERVICE_NAME)
	ShowBoxService sbService;
	
	@Resource(name = ShowPointService.SERVICE_NAME)
	ShowPointService spService;
	
	
	
	Article article=new Article();
	
	
	
	@Override
	public Article getModel() {
		// TODO Auto-generated method stub
		return article;
	}
	
	

	
	
	
	/**
	 * 返回添加文章的界面
	 * @return
	 */
	public String goAddArticle(){
		List<ShowBox> sbBoxs = sbService.findAllBox();
		request.setAttribute("sbBoxs", sbBoxs);
		List<ArticleCategory> acList = acService.findAllNoPage2();
		request.setAttribute("cates", acList);
		return "addArticle";
	}
	
	
	
	public String addArticle(){
		ArticleCategory cate = acService.findByID(request.getParameter("category"));
		// 基础设置
		article.setArticleCategory(cate);
		article.setArTime(CU.getSqlTime());
		article.setArViewCount(0);
		article.setArLike(0);
		article.setArComentCount(0);
		article.setPuser((Puser)request.getSession().getAttribute("user"));
		if (file1 != null) {// 如果用户上传了图片
			// 设置文章缩略图
			article.setArAuthor(CU.writeFile(file1, "article", file1FileName, request.getContextPath()));
		} else if (request.getParameter("autoImg") != null) {// 自动从文章中截取图片
			String artCount = article.getArContent();
			String dist = null;
			Pattern p = Pattern.compile("src=\".*?\"");
			Matcher m = p.matcher(artCount);
			while (m.find()) {
				String g = m.group();
				dist = g;
			}
			if (dist == null) {
				article.setArAuthor(request.getContextPath() + "/images/default.jpg");
			} else {
				article.setArAuthor( dist.substring(5, dist.length() - 1));
			}
		} else {
			article.setArAuthor(request.getContextPath() + "/images/default.jpg");
		}
		artService.save(article);
		// 设置文章的展柜
		String[] sbBoxs = request.getParameterValues("sbBoxs");
		if (sbBoxs != null) {
			for (int i = 0; i < sbBoxs.length; i++) {
				ShowBox sbBox = sbService.findByID(Integer
						.parseInt(sbBoxs[i]));
				ShowPoint sp = new ShowPoint();
				sp.setShowBox(sbBox);
				sp.setSpOrderBy(CU.getSqlTime());
				sp.setSpTargetId(article.getArId() + "");
				spService.save(sp);
			}
		}
		return CU.goMsgPage(request, "成功发表一篇文章！", "ok",
				"manage/articleManager_goAddArticle.do");
	}
	
	
	
	public String serchArticle() {
		PageBean<Article> pageBean = new PageBean<Article>();
		PageBean.initPageBean(pageBean, request, "");
		
		String title = request.getParameter("articleTitel");
		String category = request.getParameter("category");
		String actType = request.getParameter("artType");
		
		pageBean=artService.serch(pageBean,title,category,actType);

		List<ArticleCategory> acShowList = acService.findAllNoPage();
		request.setAttribute("currentCate", category);
		request.setAttribute("keyWord", title);
		request.setAttribute("currentType", actType);
		
		CU.deBugPrintln("ACtype= "+actType);
		
		
		request.setAttribute("pageBean", pageBean);
		request.setAttribute("cates", acShowList);
		return "articleList";
	}
	
	public String editorArticle(){
		
		List<ShowBox> sbBoxs = sbService.findAllBox();
		request.setAttribute("sbBoxs", sbBoxs);
		List<ArticleCategory> acList = acService.findAllNoPage2();
		request.setAttribute("cates", acList);
		request.setAttribute("article", artService.findByID(request.getParameter("id")));
		
		// 根据文章找到展柜
				List<ShowPoint> spList = spService.findPointByTargetId(request.getParameter("id"));
				request.setAttribute("spList", spList);
				
		return "editorArticle";
	}

	public String updateArticle(){
		
		Article oldArticle=artService.findByID(article.getArId());
		
		ArticleCategory cate = acService.findByID(request.getParameter("category"));
		
		
		if(!cate.getAcId().equals(oldArticle.getArticleCategory().getAcId())){
			article.setArticleCategory(cate);
		}else{
			article.setArticleCategory(oldArticle.getArticleCategory());
		}
		
		article.setArTime(oldArticle.getArTime());
		article.setArComentCount(oldArticle.getArComentCount());
		article.setArLike(oldArticle.getArLike());
		article.setArViewCount(oldArticle.getArViewCount());
		article.setPuser(oldArticle.getPuser());

		
		if (file1 != null) {// 如果用户上传了图片
			// 设置文章缩略图
			article.setArAuthor(CU.writeFile(file1, "article", file1FileName, request.getContextPath()));
		} else if (request.getParameter("autoImg") != null) {// 自动从文章中截取图片
			String artCount = article.getArContent();
			String dist = null;
			Pattern p = Pattern.compile("src=\".*?\"");
			Matcher m = p.matcher(artCount);
			while (m.find()) {
				String g = m.group();
				dist = g;
			}
			if (dist == null) {
				article.setArAuthor(oldArticle.getArAuthor());
			} else {
				article.setArAuthor( dist.substring(5, dist.length() - 1));
			}
		} else {
			article.setArAuthor(oldArticle.getArAuthor());
		}

		artService.update(article);
		
		// 重新设置展位
				spService.delPointByTargetId(article.getArId());
				String[] sbBoxs = request.getParameterValues("sbBoxs");
				if (sbBoxs != null) {
					for (int i = 0; i < sbBoxs.length; i++) {
						ShowBox sbBox = sbService.findByID(sbBoxs[i]);
						ShowPoint sp = new ShowPoint();
						sp.setShowBox(sbBox);
						sp.setSpOrderBy(CU.getSqlTime());
						sp.setSpTargetId(article.getArId() + "");
						spService.save(sp);
					}
				}

		
		return CU.goMsgPage(request, "文章已经完成更新", "ok",
				"manage/articleManager_goAddArticle.do");
		
	}
	
	
	
	public String deleteArticle() {
		artService.deleteById(request.getParameter("id"),request.getContextPath());
		ajaxPrint("删除成功！");
		return null;
	}
	
	
	public String deleteArticles() {
		String[] ids = request.getParameter("ids").split(",");
		for (int i = 0; i < ids.length; i++) {
			artService.deleteById(ids[i],	request.getContextPath());
		}
		ajaxPrint("成功删除"+ids.length+"篇文章！");
		return null;
	}
	
	
	
	
	
	
	
	
	
	
	
	/**
	 * 把这个file1名称改了就不对 我也不知道为什么！
	 */
	private File file1;
	private String file1FileName;
	private String file1ContentType;

	public File getFile1() {
		return file1;
	}

	public void setFile1(File file1) {
		this.file1 = file1;
	}

	public String getFile1FileName() {
		return file1FileName;
	}

	public void setFile1FileName(String file1FileName) {
		this.file1FileName = file1FileName;
	}

	public String getFile1ContentType() {
		return file1ContentType;
	}

	public void setFile1ContentType(String file1ContentType) {
		this.file1ContentType = file1ContentType;
	}
}
